var app = getApp();
Page({
    data: {
        selected: true,
        selected1: false,
        selected2: false,
        selected3: false,
        selected4: false,
        data: '',
        navtype: '',
        code: '',
        user_type: ''

    },
    selected: function(e) {
        var type = e.target.dataset.type;
        this.setData({
            selected1: false,
            selected2: false,
            selected3: false,
            selected4: false,
            selected: true,
            navtype: type

        })
        this.order(type)
    },
    selected1: function(e) {
        var type = e.target.dataset.type;
        this.setData({
            selected: false,
            selected4: false,
            selected3: false,
            selected2: false,
            selected1: true,
            navtype: type
        })
        this.order(type)
    },
    selected2: function(e) {
        var type = e.target.dataset.type;
        this.setData({
            selected: false,
            selected4: false,
            selected3: false,
            selected2: true,
            selected1: false,
            navtype: type
        })
        this.order(type)
    },
    selected3: function(e) {
        var type = e.target.dataset.type;
        this.setData({
            selected: false,
            selected4: false,
            selected3: true,
            selected2: false,
            selected1: false,
            navtype: type
        })
        this.order(type)
    },
    selected4: function(e) {
        var type = e.target.dataset.type;
        this.setData({
            selected: false,
            selected4: true,
            selected3: false,
            selected2: false,
            selected1: false,
            navtype: type
        })
        this.order(type)
    },
    order(type) {
        var that = this;
        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/order/orderlist',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        type: parseInt(that.data.navtype),
                        page: 1,
                        user_type: that.data.user_type
                    },
                    success: function(res) {
                        if (res.data.code == 1) {
                            that.setData({
                                data: res.data.data,
                                code: res.data.code
                            })
                        } else {
                            that.setData({
                                code: res.data.code
                            })
                        }
                    }
                })
            },
        })
    },
    //确认收货
    confirmreceive: function(e) {
        var type = e.target.dataset.type;
        var user_type = this.data.user_type
        var that = this;
        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/order/confirmreceive',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        trade_no: type

                    },
                    success: function(res) {
                        if (res.data.code == 1) {
                            wx.showToast({
                                title: res.data.msg,
                                icon: '',
                                duration: 2000,
                                mask: true
                            })
                            that.onLoad({ user_type })
                        }
                    }
                })
            },
        })
    },
    // 删除订单
    dellorder: function(e) {
        var type = e.target.dataset.type;
        var that = this;
        var user_type = this.data.user_type
        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/order/delorder',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        trade_no: type

                    },
                    success: function(res) {
                        if (res.data.code == 1) {
                            wx.showToast({
                                title: res.data.msg,
                                icon: '',
                                duration: 2000,
                                mask: true
                            })
                            that.onLoad({ user_type })
                        }
                    }
                })
            },
        })
    },
    // 付款
    pay: function(e) {
        var type = e.target.dataset.type;
        var that = this;
        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/order/order_price',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        trade_no: type
                    },
                    success: function(res) {
                        var list = res.data;

                        if (list.code == 1) {
                            wx.navigateTo({
                                url: `../pay-style/pay-style?money=${list.data.price}&order_no=${list.data.trade_no}`,
                            })
                        }
                    }
                })
            }
        })
    },
    // 提醒付款
    txfk: function(e) {
        var order_id = e.target.id;
        var that = this;
        var user_type = this.data.user_type
        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/order/remind_dev',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        order_id: parseInt(order_id),
                        type: 2
                    },
                    success: function(res) {
                        if (res.data.code == 1) {
                            wx.showToast({
                                title: res.data.msg,
                                icon: '',
                                duration: 2000,
                                mask: true
                            })
                            that.onLoad({user_type})
                        } else {
                            wx.showToast({
                                title: res.data.msg,
                                icon: '',
                                duration: 2000,
                                mask: true
                            })
                        }

                    }
                })
            }
        })
    },
    // 待发货提醒发货
    txfh: function(e) {
        var order_id = e.target.id;
        var that = this;
        var user_type = this.data.user_type
        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/order/remind_dev',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        order_id: parseInt(order_id),
                        type: 1
                    },
                    success: function(res) {
                        if (res.data.code == 1) {
                            wx.showToast({
                                title: res.data.msg,
                                icon: '',
                                duration: 2000,
                                mask: true
                            })
                            that.onLoad({ user_type })
                        } else {
                            wx.showToast({
                                title: res.data.msg,
                                icon: '',
                                duration: 2000,
                                mask: true
                            })
                        }

                    }
                })
            }
        })
    },
    onLoad: function(options) {
        if (options.user_type) {
            this.setData({
                user_type: parseInt(options.user_type)
            })
            this.order()
        } else {
            this.order()
        }
    },
})